package com.uchengwang.ui.cm_ui.umsg.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.OSSManager;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.umsg.bean.WantChatBean;
import com.uchengwang.ui.cm_ui.umsg.bean.WantChooseImgBean;
import com.uchengwang.ui.cm_ui.umsg.http.ChatHttpManager;
import com.uchengwang.ui.utils.ImageManagerUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2017/12/14.
 * 编辑享聊头像图片
 */

public class WantChatEditorImgActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private ImageView bigImg, head_1, head_2;
    private Button bt;
    private String bigImgS, hean_1S, head_2S;//选择的本地图片

    private int type;//0刚进入享聊, 选择图片, 会从上个界面带过来一个昵称数据 1已进入享聊, 修改图片信息 2审核被拒 重新提交
    private String nickName;//上个界面起的享聊昵称
    private WantChatBean bean;//修改图片信息时, 用到
    private List<String> beanImgs;//个人的头像

    private ArrayList<WantChooseImgBean> fileChoose = new ArrayList<>();
    private WantChooseImgBean bigImgBean, head1ImgBean, head2ImgBean;

    private String ossCover, ossResultString;//提交到oss城的图片

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_want_chat_img);
        EventBus.getDefault().register(this);

        type = getIntent().getIntExtra(BaseConfig.TYPE, -1);
        switch (type){
            case 0://刚进入享聊 选择图片
                nickName = getIntent().getStringExtra(BaseConfig.NICK_NAME);
                break;
            case 1://修改图片信息
            case 2://重新提交
                bean = (WantChatBean) getIntent().getSerializableExtra(BaseConfig.BEAN);
                break;
        }

        setTitle("大头贴");
//        setRightText("帮助", new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//            }
//        });

        initView();
    }

    private void initView(){
        bigImg = (ImageView) findViewById(R.id.wantChooseImg_bigImg);
        bigImg.setOnClickListener(this);
        head_1 = (ImageView) findViewById(R.id.wantChooseImg_head_1);
        head_1.setOnClickListener(this);
        head_2 = (ImageView) findViewById(R.id.wantChooseImg_head_2);
        head_2.setOnClickListener(this);

        bt = (Button) findViewById(R.id.wantChooseImg_bt);
        bt.setOnClickListener(this);

        switch (type){
            case 0://刚进去享聊

                break;
            case 1://修改图片
                bt.setText("确定修改");
                break;
            case 2://重新提交
                bt.setText("重新提交");
                break;
        }

        ImageManager.getInstance().loadImage(this, bigImg, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.NARMOL);
        ImageManager.getInstance().loadImage(this, head_1, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.NARMOL);
        ImageManager.getInstance().loadImage(this, head_2, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.NARMOL);

        if (bean != null ){
            beanImgs = bean.getHeads();
            for (int i=0; i<beanImgs.size(); i++){
                String url = beanImgs.get(i);
                switch (i){
                    case 0:
                        if (!TextUtils.isEmpty(url)){
                            ImageManager.getInstance().setRoundSize(5).loadImage(this, bigImg, url, ImageManager.ImageType.ROUND);
                        }else {
                            ImageManager.getInstance().setRoundSize(5).loadImage(this, bigImg, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
                        }
                        break;
                    case 1:
                        if (!TextUtils.isEmpty(url)){
                            ImageManager.getInstance().setRoundSize(5).loadImage(this, head_1, url, ImageManager.ImageType.ROUND);
                        }else {
                            ImageManager.getInstance().setRoundSize(5).loadImage(this, head_1, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
                        }
                        break;
                    case 2:
                        if (!TextUtils.isEmpty(url)){
                            ImageManager.getInstance().setRoundSize(5).loadImage(this, head_2, url, ImageManager.ImageType.ROUND);
                        }else {
                            ImageManager.getInstance().setRoundSize(5).loadImage(this, head_2, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
                        }
                        break;
                }
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.wantChooseImg_bigImg:
                chooseImage(1);
                break;
            case R.id.wantChooseImg_head_1:
                chooseImage(2);
                break;
            case R.id.wantChooseImg_head_2:
                chooseImage(3);
                break;
            case R.id.wantChooseImg_bt:
                fileChoose.clear();
                if (bean != null && bean.getHeads() != null && bean.getHeads().size() >0){
                    List<String> netUrls = bean.getHeads();//为了 修改图片方便
                    for (int i=0; i< netUrls.size(); i++){
                        switch (i){
                            case 0:
                                bigImgBean = new WantChooseImgBean(1, netUrls.get(i), false);
                                break;
                            case 1:
                                head1ImgBean = new WantChooseImgBean(2, netUrls.get(i), false);
                                break;
                            case 2:
                                head2ImgBean = new WantChooseImgBean(2, netUrls.get(i), false);
                                break;
                        }
                    }
                }

                if (!TextUtils.isEmpty(bigImgS)){
                    if (bigImgBean == null){
                        bigImgBean = new WantChooseImgBean(1, bigImgS, true);
                    }else {
                        bigImgBean.setLocal(true);
                        bigImgBean.setFileUrl(bigImgS);
                    }
                }

                if (bigImgBean != null){
                    fileChoose.add(bigImgBean);
                }

                if (!TextUtils.isEmpty(hean_1S)){
                    if (head1ImgBean == null){
                        head1ImgBean = new WantChooseImgBean(2, hean_1S, true);
                    }else {
                        head1ImgBean.setLocal(true);
                        head1ImgBean.setFileUrl(hean_1S);
                    }
                }

                if (head1ImgBean != null){
                    fileChoose.add(head1ImgBean);
                }

                if (!TextUtils.isEmpty(head_2S)){
                    if (head2ImgBean == null){
                        head2ImgBean = new WantChooseImgBean(2, head_2S, true);
                    }else {
                        head2ImgBean.setLocal(true);
                        head2ImgBean.setFileUrl(head_2S);
                    }
                }

                if (head2ImgBean != null){
                    fileChoose.add(head2ImgBean);
                }

                if (fileChoose.size() <=0){
                    UCommonUtil.getUtil().showToast(this, "请选择至少一张图片");
                    return;
                }

                //0刚进入享聊, 选择图片, 会从上个界面带过来一个昵称数据 1已进入享聊, 修改图片信息 2审核被拒 重新提交
                switch (type){
                    case 0:
                    case 2:
//                        Intent intent = new Intent(this, WantChatSignActivity.class);
//                        intent.putExtra(BaseConfig.NICK_NAME, nickName);
//                        intent.putExtra(BaseConfig.IMGS, fileChoose);
//                        intent.putExtra(BaseConfig.TYPE, type);
//                        if (bean != null){
//                            intent.putExtra(BaseConfig.BEAN, bean);
//                        }
//                        startActivity(intent);
                        break;
                    case 1:
                        changeInfo();
                        break;
                }

                break;
        }
    }

    /**
     * 1大图片 2右上 3右下
     * */
    private void chooseImage(final int type){
        UCommonUtil.getUtil().openAndCrop(this, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                UCommonUtil.getUtil().showToast(WantChatEditorImgActivity.this, "图片错误");
            }

            @Override
            public void onResp(int status, String msg, String headUrl) {
                if (!TextUtils.isEmpty(headUrl)){
                    switch (type){
                        case 1:
                            bigImgS = headUrl;
                            ImageManager.getInstance().setRoundSize(5).loadImage(WantChatEditorImgActivity.this, bigImg, headUrl, ImageManager.ImageType.ROUND);
                            break;
                        case 2:
                            hean_1S = headUrl;
                            ImageManager.getInstance().setRoundSize(5).loadImage(WantChatEditorImgActivity.this, head_1, headUrl, ImageManager.ImageType.ROUND);
                            break;
                        case 3:
                            head_2S = headUrl;
                            ImageManager.getInstance().setRoundSize(5).loadImage(WantChatEditorImgActivity.this, head_2, headUrl, ImageManager.ImageType.ROUND);
                            break;
                    }
                }
            }
        });
    }

    /**
     * 修改享聊信息
     * */
    private void changeInfo(){
        if (!TextUtils.isEmpty(ossCover) && !TextUtils.isEmpty(ossResultString)){
            postInfo();
        }else {
            List<String> imgs = new ArrayList<>();
            if (fileChoose != null && fileChoose.size() >0){
                LoadingUtils.getUtils().loading(this);//不知道为啥 加载显示的不放着 oss会抛异常
                for (WantChooseImgBean b :fileChoose){
                    if (b.isLocal()){
                        imgs.add(b.getFileUrl());//只把修改的图片拿出来提交
                    }
                }
                if (imgs .size() <=0){
                    LoadingUtils.getUtils().hide();
                    UCommonUtil.getUtil().showToast(WantChatEditorImgActivity.this, "没有图片被改动");
                    WantChatEditorImgActivity.this.finish();
                    return;
                }
                ImageManagerUtils.getUtils().compressAndPost(this, OSSManager.OSS_SHARE, imgs, 300, false, new ImageManagerUtils.ImageCompressListener() {
                    @Override
                    public void onResult(int stutas, String cover, String result) {
                        //因为之前有过图片 替换或者删除
                        List<String> results = Arrays.asList(result.split(";"));
                        for (int i=0; i <results.size(); i++){//将对于修改的图片 上传成功后  赋值给对于的位置
                            for (int j=0; j < fileChoose.size(); j++){
                                WantChooseImgBean b = fileChoose.get(j);
                                if (b.isLocal()){
                                    //说明是本地图片, 替换为oss的图片
                                    b.setFileUrl(results.get(i));
                                    b.setLocal(false);
                                    break;
                                }
                            }
                        }

                        ossCover = fileChoose.get(0).getFileUrl();
                        ossResultString = ChatHttpManager.getManager().getHeadString(fileChoose);
                        postInfo();

                    }
                });
            }
        }

    }

    /**
     * 提交信息
     * */
    private void postInfo(){
        //将提交后的图片 提交到服务器
        MapRequest map = new MapRequest();
        map.put(BaseConfig.CHAT_TYPE, 1);
        map.put(BaseConfig.CHAT_AVATAR, ossCover);
        map.put(BaseConfig.CHAT_IMGS, ossResultString);
        ChatHttpManager.getManager().changeWantInfo(WantChatEditorImgActivity.this, map, new OnResultListener() {
            @Override
            public void onResult(int stutas, String msg, int total, String data) {
                if (stutas ==0){
                    UCommonUtil.getUtil().showToast(WantChatEditorImgActivity.this, "修改成功");
                    if (bean != null){
                        bean.setChatAvatar(fileChoose.get(0).getFileUrl());
                        bean.setChatImgs(ossResultString);
                        EventBus.getDefault().post(bean);
                    }
                    WantChatEditorImgActivity.this.finish();
                }else {
                    UCommonUtil.getUtil().showToast(WantChatEditorImgActivity.this, msg);
                }
            }
        });
    }

    /**
     *首次进入 完成进入享聊 把注册享聊流程的界面finish掉
     * true 完成享聊的认证
     * */
    public void onEvent( Boolean b){
        if (b != null && b){
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
